package eu.dominicum.ft.ui.web.jsf.view.bean;

import eu.dominicum.ft.pd.Person;
import eu.dominicum.ft.pd.boundary.GeneralManagement;
import eu.dominicum.ft.pd.mao.MakePersonMAO;
import org.apache.myfaces.trinidad.context.RequestContext;

import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;

/**
 * Backing bean for the make-person-dialog view.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@ViewScoped
@ManagedBean(name = "makePersonDialog")
public final class MakePersonDialogBean {

    /**
     * MakePersonMAO.
     */
    @Inject
    private MakePersonMAO makePersonMAO;

    /**
     * GeneralManagement.
     */
    @Inject
    private GeneralManagement generalManagement;

    /**
     * Action event handler that fires when the Maak button is clicked.
     *
     * @param actionEvent -
     */
    public void makePerson(final ActionEvent actionEvent) {

        try {
            Person newPerson = this.generalManagement.makePerson(this.makePersonMAO);
            RequestContext.getCurrentInstance().returnFromDialog(newPerson, null);
        } catch (EJBException eex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    eex.getMessage(), null));
        }
    }

    /**
     * .
     *
     * @return MakePersonMAO
     */
    public MakePersonMAO getMakePersonMAO() {
        return makePersonMAO;
    }
}
